import pymysql
from time import time

DB_USER = 'user_rpa'
DB_PASSWORD = '123456'
DB_HOST = '192.168.123.238'
DB_PORT = '3306'
DB_NAME = 'rpa'
DB_CHARSET = 'utf8mb4'


class MysqlUtils:

    def __init__(self):
        self.connection = pymysql.connect(
            host=DB_HOST,
            user=DB_USER,
            password=DB_PASSWORD,
            database=DB_NAME,
            port=int(DB_PORT),
            charset=DB_CHARSET
        )

    def __del__(self):
        if self.connection:
            self.connection.close()

    def get_marketplace_id_and_merchant_id(self, browserOauth):
        target_cursor = self.connection.cursor()
        sql = """SELECT merchantID, marketplace_id 
                            FROM amz_store 
                            WHERE browserOauth = %s"""  # 使用参数化查询
        target_cursor.execute(sql, (browserOauth,))
        ret = target_cursor.fetchone()
        if ret:
            return ret[0], ret[1]
        return None, None

    def get_all_store_list(self):
        target_cursor = self.connection.cursor()
        sql = """select browserOauth from amz_store"""
        target_cursor.execute(sql)
        ret = target_cursor.fetchall()
        store_id_list = []
        if not ret:
            return store_id_list
        store_id_list = [browserOauth_obj[0] for browserOauth_obj in ret]
        return store_id_list
        # print(browserOauth_list)


if __name__ == '__main__':
    m = MysqlUtils()
    # m.get_marketplace_id_and_merchant_id('OFNuckpabjNmRk9zNWxTNmNzczRkZz09')
    m.get_all_store_list()
